package thesis.filesharing.web.auth;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

public class AuthUtil {
	
	public static boolean containAuthority(AuthenticationUserDetails user, Roles r) {
		if (user == null || r == null) {
			return false;
		}
		if (user.getAuthorities() != null && !user.getAuthorities().isEmpty()) {
			for (GrantedAuthority a : user.getAuthorities()) {
				if (r.getRole().equalsIgnoreCase(a.getAuthority())) {
					return true;
				}
			}
		}
		return false;
	}
	
	public static String getCurrentAuthenticatedUsername() {
		Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (obj instanceof AuthenticationUserDetails) {
			return ((AuthenticationUserDetails) obj).getUsername();
		}
		return null;
	}
}
